package jax.lang;
class JObject implements haxe.Public {
	var _type:String;
	var _fields:Array<JField>;
	var _w:Bool;
	function new() {
		_fields = [];
		_w = false;
		_type = "java/lang/Object";
	}
	static function init(o:JObject, c:JClass) {
		for(f in c.cfields) {
			var nm = JVM.toNativeValue(JVM.toValue(c.const_pool[f.name_index]));
			var ty = JVM.toNativeValue(JVM.toValue(c.const_pool[f.descriptor_index]));
			var af = f.access_flags;
			o._fields.push(new JField(nm, ty, af));
		}
	}
	function notify() {
		_w = true;
	}
	function notifyAll() {
		_w = true;
	}
	function wait() {
		while(!_w){}
	}
	function toString() {
		return _type;
	}
	function getField(cn:JConstant):JField {
		var c = JVM.classes.get(_type);
		var n=null, t=null;
		switch(cn) {
			case fieldref(na, ta):
				switch(c.const_pool[ta]) {
					case nameandtype(ni, ti):
						n = JVM.toNativeValue(JVM.toValue(c.const_pool[ni]));
						t = JVM.toNativeValue(JVM.toValue(c.const_pool[ti]));
					default:
				}
			default: throw "Invalid field reference";
		}
		#if debug
		trace("Field Name: "+n);
		trace("Field Type: "+t);
		#end
		for(f in _fields) {
			if(f.name == n && f.type == t) {
				#if debug
					trace("Match!");
				#end
				return f;
			}
		}
		return null;
	}
	function clone() {
		var obj = new JObject();
		obj._type = this._type;
		for(f in this._fields) {
			var nf = new JField(f.name, f.type, f.access_flags);
			nf.value = f.value;
			obj._fields.push(nf);
		}
		return obj;
	}
}
class JField implements haxe.Public {
	var name:String;
	var type:String;
	var value:JValue;
	var access_flags:Int;
	function new(n, t, ?af=0) {
		name = n;
		type = t;
		access_flags = af;
		value = JValue._void;
	}
}